home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap09 / Environ / Environ.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  5.6 KB  |  157 lines

  1. /*----------------------------------------
  2.    ENVIRON.C -- Environment List Box
  3.                 (c) Charles Petzold, 1998
  4.   ----------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. #define ID_LIST     1
  9. #define ID_TEXT     2
  10.  
  11. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  12.  
  13. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  14.                     PSTR szCmdLine, int iCmdShow)
  15. {
  16.      static TCHAR szAppName[] = TEXT ("Environ") ;
  17.      HWND         hwnd ;
  18.      MSG          msg ;
  19.      WNDCLASS     wndclass ;
  20.      
  21.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  22.      wndclass.lpfnWndProc   = WndProc ;
  23.      wndclass.cbClsExtra    = 0 ;
  24.      wndclass.cbWndExtra    = 0 ;
  25.      wndclass.hInstance     = hInstance ;
  26.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  27.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  28.      wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1) ;
  29.      wndclass.lpszMenuName  = NULL ;
  30.      wndclass.lpszClassName = szAppName ;
  31.      
  32.      if (!RegisterClass (&wndclass))
  33.      {
  34.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  35.                       szAppName, MB_ICONERROR) ;
  36.           return 0 ;
  37.      }
  38.      
  39.      hwnd = CreateWindow (szAppName, TEXT ("Environment List Box"),
  40.                           WS_OVERLAPPEDWINDOW,
  41.                           CW_USEDEFAULT, CW_USEDEFAULT,
  42.                           CW_USEDEFAULT, CW_USEDEFAULT,
  43.                           NULL, NULL, hInstance, NULL) ;
  44.      
  45.      ShowWindow (hwnd, iCmdShow) ;
  46.      UpdateWindow (hwnd) ;
  47.      
  48.      while (GetMessage (&msg, NULL, 0, 0))
  49.      {
  50.           TranslateMessage (&msg) ;
  51.           DispatchMessage (&msg) ;
  52.      }
  53.      return msg.wParam ;
  54. }
  55.  
  56. void FillListBox (HWND hwndList) 
  57. {
  58.      int     iLength ;
  59.      TCHAR * pVarBlock, * pVarBeg, * pVarEnd, * pVarName ;
  60.  
  61.      pVarBlock = GetEnvironmentStrings () ;  // Get pointer to environment block
  62.  
  63.      while (*pVarBlock)
  64.      {
  65.           if (*pVarBlock != '=')   // Skip variable names beginning with '='
  66.           {
  67.                pVarBeg = pVarBlock ;              // Beginning of variable name
  68.                while (*pVarBlock++ != '=') ;      // Scan until '='
  69.                pVarEnd = pVarBlock - 1 ;          // Points to '=' sign
  70.                iLength = pVarEnd - pVarBeg ;      // Length of variable name
  71.  
  72.                     // Allocate memory for the variable name and terminating
  73.                     // zero. Copy the variable name and append a zero.
  74.  
  75.                pVarName = calloc (iLength + 1, sizeof (TCHAR)) ;
  76.                CopyMemory (pVarName, pVarBeg, iLength * sizeof (TCHAR)) ;
  77.                pVarName[iLength] = '\0' ;
  78.  
  79.                     // Put the variable name in the list box and free memory.
  80.  
  81.                SendMessage (hwndList, LB_ADDSTRING, 0, (LPARAM) pVarName) ;
  82.                free (pVarName) ;
  83.           }
  84.           while (*pVarBlock++ != '\0') ;     // Scan until terminating zero
  85.      }
  86.      FreeEnvironmentStrings (pVarBlock) ;
  87. }
  88.  
  89. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  90. {
  91.      static HWND  hwndList, hwndText ;
  92.      int          iIndex, iLength, cxChar, cyChar ;
  93.      TCHAR      * pVarName, * pVarValue ;
  94.  
  95.      switch (message)
  96.      {
  97.      case WM_CREATE :
  98.           cxChar = LOWORD (GetDialogBaseUnits ()) ;
  99.           cyChar = HIWORD (GetDialogBaseUnits ()) ;
  100.  
  101.                // Create listbox and static text windows.
  102.           
  103.           hwndList = CreateWindow (TEXT ("listbox"), NULL,
  104.                               WS_CHILD | WS_VISIBLE | LBS_STANDARD,
  105.                               cxChar, cyChar * 3,
  106.                               cxChar * 16 + GetSystemMetrics (SM_CXVSCROLL),
  107.                               cyChar * 5,
  108.                               hwnd, (HMENU) ID_LIST,
  109.                               (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
  110.                               NULL) ;
  111.           
  112.           hwndText = CreateWindow (TEXT ("static"), NULL,
  113.                               WS_CHILD | WS_VISIBLE | SS_LEFT,
  114.                               cxChar, cyChar, 
  115.                               GetSystemMetrics (SM_CXSCREEN), cyChar,
  116.                               hwnd, (HMENU) ID_TEXT,
  117.                               (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
  118.                               NULL) ;
  119.  
  120.           FillListBox (hwndList) ;
  121.           return 0 ;
  122.           
  123.      case WM_SETFOCUS :
  124.           SetFocus (hwndList) ;
  125.           return 0 ;
  126.           
  127.      case WM_COMMAND :
  128.           if (LOWORD (wParam) == ID_LIST && HIWORD (wParam) == LBN_SELCHANGE)
  129.           {
  130.                     // Get current selection.
  131.  
  132.                iIndex  = SendMessage (hwndList, LB_GETCURSEL, 0, 0) ;
  133.                iLength = SendMessage (hwndList, LB_GETTEXTLEN, iIndex, 0) + 1 ;
  134.                pVarName = calloc (iLength, sizeof (TCHAR)) ;
  135.                SendMessage (hwndList, LB_GETTEXT, iIndex, (LPARAM) pVarName) ;
  136.  
  137.                     // Get environment string.
  138.  
  139.                iLength = GetEnvironmentVariable (pVarName, NULL, 0) ;
  140.                pVarValue = calloc (iLength, sizeof (TCHAR)) ;
  141.                GetEnvironmentVariable (pVarName, pVarValue, iLength) ;
  142.  
  143.                     // Show it in window.
  144.                
  145.                SetWindowText (hwndText, pVarValue) ;
  146.                free (pVarName) ;
  147.                free (pVarValue) ;
  148.           }
  149.           return 0 ;
  150.  
  151.      case WM_DESTROY :
  152.           PostQuitMessage (0) ;
  153.           return 0 ;
  154.      }
  155.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  156. }
  157.